package com.github.prontera.controller;

import io.swagger.annotations.ApiOperation;

import java.time.OffsetDateTime;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.github.prontera.ApiVersion;
import com.github.prontera.domain.Task;
import com.github.prontera.model.response.ObjectDataResponse;
import com.github.prontera.model.response.Response;
import com.github.prontera.service.TaskService;
import com.github.prontera.util.Page;

/**
 * 技能标签
 * @author Alex
 */
@RestController
@RequestMapping(value = ApiVersion.MANAGE_V1_PREFIX + "/task", produces = { MediaType.APPLICATION_JSON_UTF8_VALUE }, consumes = { MediaType.APPLICATION_JSON_UTF8_VALUE })
public class TaskController {

	@Autowired
	private TaskService taskService;

	@ApiOperation(value = "查询任务列表", notes = "查询任务列表")
	@RequestMapping(value = "findTaskList", method = RequestMethod.POST)
	@ResponseBody
	public ObjectDataResponse<Page<Task>> findTaskList(@RequestBody Task task) {
		Page<Task>  page = taskService.pageTask(task);
		return new ObjectDataResponse<Page<Task>>(page);
	}
	
	@ApiOperation(value = "添加任务", notes = "添加任务")
	@RequestMapping(value = "saveTask", method = RequestMethod.POST)
	@ResponseBody
	public Response saveTask(@RequestBody Task task) {
	  task.setCreateTime(OffsetDateTime.now());
	  
	  int success =  taskService.insert(task);
	  return new ObjectDataResponse<Object>(success);
	}
	
	@ApiOperation(value = "修改任务", notes = "修改任务")
	@RequestMapping(value = "updateTask", method = RequestMethod.POST)
	@ResponseBody
	public Response updateTask(@RequestBody Task task) {
		
	   if(task!=null){
		   task.setUpdateTime(OffsetDateTime.now());
	   }
	
	   int success =  taskService.update(task);
	   return new ObjectDataResponse<Object>(success);
	}
	
	@ApiOperation(value = "修改任务", notes = "修改任务")
	@RequestMapping(value = "deleteTask", method = RequestMethod.POST)
	@ResponseBody
	public Response deleteTask(@RequestBody Task task) {
	
	   int success =  taskService.deleteTask(task);
	   return new ObjectDataResponse<Object>(success);
	}

	@ApiOperation(value = "获取任务对象", notes = "获取任务对象")
	@RequestMapping(value = "getTaskById", method = RequestMethod.POST)
	@ResponseBody
	public Response getTaskById(@RequestBody Task task) {
		
	   task = taskService.findOne(task);
		
	   return new ObjectDataResponse<Task>(task);
	}
}
